/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package libraryproject;

import database_objects.Book;
import database_objects.Department;
import database_objects.EditionBook;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import user_client.LibrarianUserClient;

/**
 *
 * @author pawel
 */
public class EditionChoicePane extends javax.swing.JPanel {

    /**
     * Creates new form EditionChoicePane
     */
    public EditionChoicePane() {
        System.out.println("without");
        initComponents();
        this.model = (DefaultTableModel) resultTable.getModel();
        this.resultTable.getSelectionModel().addListSelectionListener(
				new MySelectionListener());
    }

    public EditionChoicePane(LibrarianUserClient client, Book currentBook,
            BooksManagementsPane booksManagementsPane, JDialog owner,
            EditionBook edition) {
        System.out.println("with");
        this.client = client;
        
        initComponents();
        this.model = (DefaultTableModel) resultTable.getModel();
        
        this.resultTable.getSelectionModel().addListSelectionListener(
				new MySelectionListener());
        this.booksManagementsPane = booksManagementsPane;
        this.owner = owner;
        this.currentEdition = edition;
        this.titleField.setText(currentEdition.getTitle());
        this.jFormattedISBNField.setText(currentEdition.getIsbn());
        this.editionNameTextField.setText(
                currentEdition.getNameOfEditionHouse());
        this.dateOfEditionTextField.setText(
                currentEdition.getDateOfPublishing());
    }

    private class MySelectionListener implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
            try {
                isbn = (String) model.getValueAt(resultTable.getSelectedRow(), 0);

                
                title = (String) model.getValueAt(resultTable.getSelectedRow(),
                        1);
                nameOfEditionHouse = (String) model.getValueAt(
                        resultTable.getSelectedRow(), 2);

                String dateOfPublishingStr = (String) model.getValueAt(resultTable.getSelectedRow(), 3);
                String idOfDepartmentSring = (String) model.getValueAt(resultTable.getSelectedRow(), 4);
                idOfDepartment = Integer.valueOf(idOfDepartmentSring);

                System.out.println("IDofDepartment");
                System.out.println(idOfDepartment);
                System.out.println(idOfDepartment + ", " + title + ", "
                        + nameOfEditionHouse);

                resultLabel.setText(isbn + " " + title + " "
                        + nameOfEditionHouse);

            } catch (ArrayIndexOutOfBoundsException e1) {
                resultLabel.setText("nie wybrano działu");
                idOfDepartment = null;
                nameOfEditionHouse = "";
                isbn = null;

            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        editionNameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dateOfEditionTextField = new javax.swing.JFormattedTextField();
        titleField = new javax.swing.JTextField();
        jFormattedISBNField = new javax.swing.JFormattedTextField();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        searchButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        resultLabel = new javax.swing.JLabel();

        jLabel4.setText("tytuł:");

        jLabel1.setText("isbn:");

        editionNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editionNameTextFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("wydawnictwo:");

        jLabel3.setText("data wydania:");

        dateOfEditionTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dateOfEditionTextFieldActionPerformed(evt);
            }
        });

        titleField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                titleFieldActionPerformed(evt);
            }
        });

        jFormattedISBNField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedISBNFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dateOfEditionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jFormattedISBNField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(editionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(editionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jFormattedISBNField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dateOfEditionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41))
        );

        jButton2.setText("zatwierdź wybór");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "isbn", "tytuł", "wydawnictwo", "data wydania", "id działu"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(resultTable);

        searchButton.setText("szukaj");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        addButton.setText("dodaj");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setText("usuń");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                    .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(removeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(searchButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(addButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(removeButton))
        );

        resultLabel.setText("Nie wybrano żadnego działu.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 426, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(resultLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(resultLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(90, 90, 90))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(92, 92, 92))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void editionNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editionNameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_editionNameTextFieldActionPerformed

    private void dateOfEditionTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dateOfEditionTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dateOfEditionTextFieldActionPerformed

    private void titleFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_titleFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_titleFieldActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            EditionBook edition = new 
                    EditionBook(isbn, nameOfEditionHouse, isbn, title, dateOfPublishing);
            
            this.currentEdition = edition;
            Department department = client.getDepartment(idOfDepartment);
            
            booksManagementsPane.setEdition(edition, department);
            
            if (owner != null) {
                owner.dispose();
            }
            // TODO add your handling code here:
        } catch (SQLException ex) {
            Logger.getLogger(EditionChoicePane.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        String name = editionNameTextField.getText();
        // String numberOfPane = dataPane.
        String isbnText = jFormattedISBNField.getText();
        String dateOfEditionText = dateOfEditionTextField.getText();
        String title = titleField.getText();
        String idOfDep;
        if (idOfDepartmentTemp != null) {
            idOfDep = idOfDepartmentTemp.toString();
        } else {
            idOfDep = "";
        }

        if ((name.length() > 0)
                && (isbnText.length() > 0)
                && (dateOfEditionText.length() > 0)
                && (title.length() > 0) && (idOfDep.length() > 0)) {

            try {
                Integer index = client.addEdition(isbnText,
                        name, dateOfEditionText, title, idOfDep);
                if (index != -1) {
                    System.out.println("INDEX:" + index);
                    String[] newRow = new String[5];
                    newRow[0] = isbn.toString();
                    newRow[1] = title;
                    newRow[2] = name;
                    newRow[3] = dateOfEditionText;
                    newRow[4] = idOfDep;
                    model.addRow(newRow);
                    model.fireTableDataChanged();
                    resultTable.revalidate();
                    resultTable.repaint();

                    resultLabel.setText("Dodano element");
                } else {
                    resultLabel.setText("Podany dział już istnieje");
                }
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
        // TODO add your handling code here:
    }//GEN-LAST:event_addButtonActionPerformed

    private void jFormattedISBNFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedISBNFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFormattedISBNFieldActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try {
            
            String idOfDepartmentString;
            if (idOfDepartment != null) {
                idOfDepartmentString = idOfDepartment.toString();
            } else {
                idOfDepartmentString = "";
            }
            //System.out.println(idOfDepartment.toString());
            ArrayList<EditionBook> editions = client.searchEditions(
                    jFormattedISBNField.getText(),
                    editionNameTextField.getText(),
                    dateOfEditionTextField.getText(),
                    titleField.getText(),
                    idOfDepartmentString);

            while (model.getRowCount() > 0) {
                try {
                    model.removeRow(0);
                } catch (Exception a) {
                    System.out.println("ERRRORRRRRRRR????");
                }

            }

            System.out.println("SIZE: "+editions.size());
            for (EditionBook edition : editions) {
                System.out.println(edition.getNameOfEditionHouse());
                String[] val = new String[5];
                val[0] = edition.getIsbn();
                val[1] = edition.getTitle();
                val[2] = edition.getNameOfEditionHouse();
                val[3] = edition.getDateOfPublishing();
                val[4] = edition.getIdOfDepartment();
                model.addRow(val);
            }

            resultTable.revalidate();

            resultTable.repaint();

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }        // TODO add your handling code here:
    }//GEN-LAST:event_searchButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        try {
            if (idOfDepartment != null) {
                client.removeElement(isbn.toString(), "wydania",
                        "isbn");
            }
            int i = 0;
            // Integer size =
            // DepartmentManagerPane.this.model.getRowCount();

            while (i < model.getRowCount()) {
                Integer valS = Integer.valueOf((String) resultTable.getValueAt(
                        i, 0));

                if (valS.equals(isbn)) {
                    model.removeRow(i);
                    model.fireTableDataChanged();

                }

                // DepartmentManagerPane.this.model.removeRow(0);
                i++;
            }
            resultTable.setModel(model);
            resultTable.revalidate();
            resultTable.repaint();

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }//GEN-LAST:event_removeButtonActionPerformed
    private LibrarianUserClient client = null;
    //data
    private String isbn = null;
    private String nameOfEditionHouse = "";
    private String dateOfPublishing;
    private String title = "";
    private Integer idOfDepartment = null;
    private DefaultTableModel model;
    private Integer idOfDepartmentTemp = null;
    private JDialog owner;
    private BooksManagementsPane booksManagementsPane = null;
    private EditionBook currentEdition = null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JFormattedTextField dateOfEditionTextField;
    private javax.swing.JTextField editionNameTextField;
    private javax.swing.JButton jButton2;
    private javax.swing.JFormattedTextField jFormattedISBNField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton removeButton;
    private javax.swing.JLabel resultLabel;
    private javax.swing.JTable resultTable;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField titleField;
    // End of variables declaration//GEN-END:variables
}
